package com.ruoyi.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.PassToken;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.locker.domain.Locker;
import com.ruoyi.locker.domain.vo.LockerVo;
import com.ruoyi.locker.domain.vo.SendParams;
import com.ruoyi.locker.service.ILockerService;
import com.ruoyi.season.domain.PlaySeasonRecord;
import com.ruoyi.season.domain.vo.PrizeInfoVo;
import com.ruoyi.season.service.IPlaySeasonRecordService;
import com.ruoyi.util.AuthUtil;
import com.ruoyi.util.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Objects;


/**
 * @author mjl
 */
@RestController
@RequestMapping("/app-api/locker")
@Api(value = "储物柜", tags = "移动端接口")
public class AppLockerController {

    @Resource
    private ILockerService lockerService;
    @Resource
    private IPlaySeasonRecordService playSeasonRecordService;





    @GetMapping("/list")
    @ApiOperation(value = "储物柜列表",notes = "type 1未发货2已发货,productType产品类型1订单商品2奖励物品3赛季" +
            ", pageNo起始页,pageSize页数")
    public R lockerInfo(@RequestParam(defaultValue = "1")Integer type ,
                          @RequestParam(defaultValue = "0")Integer productType ,
                          @RequestParam(name = "pageNo" ,defaultValue = "1") Integer pageNo
            ,@RequestParam(name = "pageSize",defaultValue = "10") Integer pageSize) {
        String loginUserId = AuthUtil.getAppUserId();
        IPage<LockerVo> page = new Page<LockerVo>(pageNo,pageSize);
        IPage<LockerVo> list = lockerService.lockerInfo(page,type,productType,loginUserId);

        return R.ok(list);
    }

    @PostMapping("/seasonUse")
    @ApiOperation(value = "赛季使用",notes = "playSeasonId赛季id,lockerId 物品id")
    public R lockerSeasonUse(String lockerId,String playSeasonId) {
        if(StringUtils.isBlank(lockerId) || StringUtils.isBlank(playSeasonId)){
           return R.fail("物品异常");
        }
        String loginUserId = AuthUtil.getAppUserId();
        PlaySeasonRecord record = playSeasonRecordService.getOne(
                new LambdaQueryWrapper<PlaySeasonRecord>()
                        .eq(PlaySeasonRecord::getPlaySeasonId,playSeasonId)
                        .eq(PlaySeasonRecord::getAppuserId,loginUserId)
                        .ne(PlaySeasonRecord::getStatus,3)
        );
        if(Objects.nonNull(record)){
           return R.fail("相同赛季请勿重复使用");
        }


        if(!lockerService.update(new LambdaUpdateWrapper<Locker>()
                .set(Locker::getIsUse,2)
                .eq(Locker::getId,lockerId)
        )){
            return R.fail("物品异常");
        }

        Date date = new Date();
        /**参加赛季记录*/
        PlaySeasonRecord playSeasonRecord = new PlaySeasonRecord();
        playSeasonRecord.setAppuserId(loginUserId);
        playSeasonRecord.setCreateTime(date);
        playSeasonRecord.setPlaySeasonId(playSeasonId);
        playSeasonRecord.setBeginTime(date);
        playSeasonRecord.setEndTime(DateUtils.getTime(date, 30));
        if(!playSeasonRecordService.save(playSeasonRecord)){
            return R.fail("赛季记录失败");
        }


        return R.ok();
    }




    @PostMapping("/send")
    @ApiOperation(value = "发货",notes = "")
    public R integralInfo(SendParams sendParams) {
        String lockerIds = sendParams.getLockerIds();
        if(StringUtils.isBlank(lockerIds)){
            return R.fail("请选择物品");
        }
        String collectName = sendParams.getCollectName();
        if(StringUtils.isBlank(collectName)){
            return R.fail("收货人不为空");
        }
        String collectMobile = sendParams.getCollectMobile();
        if(StringUtils.isBlank(collectMobile)){
            return R.fail("收货手机号不为空");
        }
        String collectAddress = sendParams.getCollectAddress();
        if(StringUtils.isBlank(collectAddress)){
            return R.fail("收货地址不为空");
        }
        String collectDetail = sendParams.getCollectDetail();
        if(StringUtils.isBlank(collectDetail)){
            return R.fail("地址详情不为空");
        }
        List<Locker> lockerList = lockerService.listByIds(Arrays.asList(lockerIds.split(",")));
        if(lockerList.size() > 0){
             for(Locker temp : lockerList){
                   temp.setCollectName(collectName);
                   temp.setCollectAddress(collectAddress);
                   temp.setCollectMobile(collectMobile);
                   temp.setCollectDetail(collectDetail);
                   temp.setIsUse(2);

              }
            if(!lockerService.updateBatchById(lockerList)){
                return R.fail("发货失败");
            }
        }
        return R.ok();
    }
}
